<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button size="small" type="primary" @click="addMenu">添加菜单</el-button>
        <el-button size="small" @click="getMenuList">刷新</el-button>
      </div>
      <el-table
        v-loading="listLoading"
        :data="menuList"
        style="width: 100%"
        size="mini"
        row-key="id"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column prop="menuName" label="菜单名称" min-width="200" />
        <el-table-column prop="path" label="路径" min-width="150" />
        <el-table-column prop="component" label="组件" min-width="150" />
        <el-table-column prop="icon" label="图标" min-width="100" />
        <el-table-column prop="sort" label="排序" min-width="80" />
        <el-table-column prop="status" label="状态" min-width="80">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
              {{ scope.row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="200" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="editMenu(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="addChildMenu(scope.row)">添加子菜单</el-button>
            <el-button size="mini" type="text" @click="deleteMenu(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑菜单对话框 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="600px"
      @close="resetForm"
    >
      <el-form ref="menuForm" :model="menuForm" :rules="menuRules" label-width="100px">
        <el-form-item label="父级菜单" prop="parentId">
          <el-cascader
            v-model="menuForm.parentId"
            :options="menuOptions"
            :props="cascaderProps"
            clearable
            placeholder="请选择父级菜单"
          />
        </el-form-item>
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="menuForm.menuName" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="菜单路径" prop="path">
          <el-input v-model="menuForm.path" placeholder="请输入菜单路径" />
        </el-form-item>
        <el-form-item label="组件路径" prop="component">
          <el-input v-model="menuForm.component" placeholder="请输入组件路径" />
        </el-form-item>
        <el-form-item label="菜单图标" prop="icon">
          <el-input v-model="menuForm.icon" placeholder="请输入图标名称" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="menuForm.sort" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="menuForm.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getMenuList, addMenu, editMenu, delMenu } from '@/api/systemadmin'

export default {
  name: 'MenuManager',
  data() {
    return {
      listLoading: false,
      menuList: [],
      dialogVisible: false,
      dialogTitle: '添加菜单',
      menuForm: {
        id: null,
        parentId: null,
        menuName: '',
        path: '',
        component: '',
        icon: '',
        sort: 0,
        status: 1
      },
      menuRules: {
        menuName: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' }
        ],
        path: [
          { required: true, message: '请输入菜单路径', trigger: 'blur' }
        ],
        component: [
          { required: true, message: '请输入组件路径', trigger: 'blur' }
        ]
      },
      menuOptions: [],
      cascaderProps: {
        value: 'id',
        label: 'menuName',
        children: 'children',
        checkStrictly: true
      }
    }
  },
  mounted() {
    this.getMenuList()
  },
  methods: {
    // 获取菜单列表
    getMenuList() {
      this.listLoading = true
      getMenuList({}).then(res => {
        this.menuList = res.data || []
        this.buildMenuOptions()
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 构建级联选择器选项
    buildMenuOptions() {
      const options = [{ id: 0, menuName: '顶级菜单', children: [] }]
      const buildTree = (list, parentId = 0) => {
        return list.filter(item => item.parentId === parentId).map(item => ({
          id: item.id,
          menuName: item.menuName,
          children: buildTree(list, item.id)
        }))
      }
      options[0].children = buildTree(this.menuList)
      this.menuOptions = options
    },
    // 添加菜单
    addMenu() {
      this.dialogTitle = '添加菜单'
      this.menuForm = {
        id: null,
        parentId: null,
        menuName: '',
        path: '',
        component: '',
        icon: '',
        sort: 0,
        status: 1
      }
      this.dialogVisible = true
    },
    // 添加子菜单
    addChildMenu(row) {
      this.dialogTitle = '添加子菜单'
      this.menuForm = {
        id: null,
        parentId: row.id,
        menuName: '',
        path: '',
        component: '',
        icon: '',
        sort: 0,
        status: 1
      }
      this.dialogVisible = true
    },
    // 编辑菜单
    editMenu(row) {
      this.dialogTitle = '编辑菜单'
      this.menuForm = { ...row }
      this.dialogVisible = true
    },
    // 删除菜单
    deleteMenu(row) {
      this.$confirm('确定要删除该菜单吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delMenu(row.id).then(() => {
          this.$message.success('删除成功')
          this.getMenuList()
        })
      })
    },
    // 提交表单
    submitForm() {
      this.$refs.menuForm.validate((valid) => {
        if (valid) {
          const formData = { ...this.menuForm }
          if (Array.isArray(formData.parentId)) {
            formData.parentId = formData.parentId[formData.parentId.length - 1] || 0
          }
          
          if (formData.id) {
            editMenu(formData).then(() => {
              this.$message.success('更新成功')
              this.dialogVisible = false
              this.getMenuList()
            })
          } else {
            addMenu(formData).then(() => {
              this.$message.success('添加成功')
              this.dialogVisible = false
              this.getMenuList()
            })
          }
        }
      })
    },
    // 重置表单
    resetForm() {
      this.$refs.menuForm && this.$refs.menuForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.divBox {
  padding: 20px;
}
</style>
